/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules.NavigationProvider;
using DotNetNuke.UI.WebControls;

using DotNetNuke.ModuleToolkit.Modules.Skinning;
using DotNetNuke.ModuleToolkit.Modules.Skinning.SkinObjects;
using DotNetNuke.ModuleToolkit.Common;

#endregion

namespace DotNetNuke.ModuleToolkit.Modules.Skinning.SkinObjects
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ReturnButton skin object displays a Return link
    /// </summary>
    /// <history>
    /// 	[cnurse]	01/20/2007	created
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    ToolboxData("<{0}:ReturnButton runat=\"server\"> </{0}:ReturnButton>")]
    public class ReturnButton : ModuleSkinObjectBase
    {
        #region Private Members

        private string _Caption;
        private string _ResourceKey;

        #endregion

        #region Protected Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The text of the control
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/20/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The Caption.")]
        public string Caption
        {
            get { return _Caption; }
            set { _Caption = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Default Css Class
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/20/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected override String DefaultCssClass
        {
            get { return "CommandButton"; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the design time text to display
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/20/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected override string DesignTimeText
        {
            get { return Constants.TEXT_Return; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A String that contains the localization resource key for the Text
        /// </summary>
        /// <history>
        /// 	[cnurse]	01/20/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Localization"),
        DefaultValue(""),
        Description("The Resource Key to use to localize the Text")]
        public string ResourceKey
        {
            get { return _ResourceKey; }
            set { _ResourceKey = value; }
        }

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderContents renders the contents of the control
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	01/20/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            bool _Show = false;
            string buttonText = DesignTimeText;
            string buttonUrl = Null.NullString;
            string image = Constants.IMAGE_Return;

            if (DesignMode)
                _Show = true;
            else
                _Show = (ParentModule != null) && !String.IsNullOrEmpty(ParentModule.Scope);

            if (_Show)
            {
                if (!DesignMode)
                {
                    buttonUrl = ParentModule.ReturnURL;

                    if (String.IsNullOrEmpty(ResourceKey))
                        buttonText = Caption;
                    else
                        buttonText = LocalizeString(ResourceKey);
                }

                if (!String.IsNullOrEmpty(buttonUrl) || DesignMode)
                {
                    //Render Hyperlink
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, buttonUrl);
                    writer.AddAttribute(HtmlTextWriterAttribute.Title, buttonText);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);

                    //Render Image
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, ResolveClientUrl(image));
                    writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);

                    //Render end of Image
                    writer.RenderEndTag();

                    //Render Hyperlink Text
                    writer.Write(buttonText);

                    //Render end of Hyperlink
                    writer.RenderEndTag();
                }
            }

        }

        #endregion

    }
}
